<?php
/**
 * eAccelerator缓存驱动器
 * @author Tongle Xu <xutongle@gmail.com> 2012-10-31
 * @copyright Copyright (c) 2003-2103 yuncms.net
 * @license http://leaps.yuncms.net
 * @version $Id: Eaccelerator.php 623 2013-07-29 03:40:03Z 85825770@qq.com $
 */
class Cache_Eaccelerator extends Cache {
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::init()
	 */
	public function init() {
		parent::init ();
		if (! function_exists ( 'eaccelerator_get' )) throw new Exception ( 'EAcceleratorCache requires PHP eAccelerator extension to be loaded, enabled or compiled with the "--with-eaccelerator-shared-memory" option.' );
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::setValue()
	 */
	protected function setValue($key, $value, $expire) {
		return eaccelerator_put ( $key, $value, $expire );
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::getValue()
	 */
	protected function getValue($key) {
		$result = eaccelerator_get ( $key );
		return $result !== NULL ? $result : false;
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::addValue()
	 */
	protected function addValue($key, $value, $expire) {
		return (NULL === eaccelerator_get ( $key )) ? $this->setValue ( $key, $value, $expire ) : false;
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::deleteValue()
	 */
	protected function deleteValue($key) {
		return eaccelerator_rm ( $key );
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::flushValues()
	 */
	public function flushValues() {
		// first, remove expired content from cache
		eaccelerator_gc ();
		// now, remove leftover cache-keys
		$keys = eaccelerator_list_keys ();
		foreach ( $keys as $key )
			$this->deleteValue ( substr ( $key ['name'], 1 ) );
		return true;
	}
}
